package net.adetech.dcweb.core.dc.rest;

import com.alibaba.fastjson.JSONObject;
import net.adetech.dcweb.core.common.entity.CommonAdminuser;
import net.adetech.dcweb.core.dc.entity.DcAlarm;
import net.adetech.dcweb.core.dc.entity.DcData;
import net.adetech.dcweb.core.dc.entity.DcInfo;
import net.adetech.dcweb.core.dc.entity.DcPeople;
import net.adetech.dcweb.core.dc.service.IDcAlarmService;
import net.adetech.dcweb.core.dc.service.IDcDataService;
import net.adetech.dcweb.core.dc.service.IDcInfoService;
import net.adetech.dcweb.core.dc.service.IDcPeopleService;
import net.adetech.web.utils.UtilsUI;
import net.adetech.web.utils.spring.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by auto on 2016/10/21.
 */
@RestController
public class DcAlarmRestController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DcAlarmRestController.class);

    @Autowired
    private IDcAlarmService iDcAlarmService;

    @Autowired
    private IDcInfoService iDcInfoService;

    @Autowired
    private IDcDataService iDcDataService;

    @Autowired
    private IDcPeopleService iDcPeopleService;


    @PutMapping(value = "/dc/alarm/read", produces = "application/json")
    public String read(HttpServletRequest request, Long id) {
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        iDcAlarmService.read(id, adminuser.getId());
        return UtilsUI.ok();
    }

    @GetMapping(value = "/dc/alarm/detail/{id}", produces = "application/json")
    public String detail(HttpServletRequest request, @PathVariable Long id) {
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        DcAlarm alarm = iDcAlarmService.find(id);
        Long dataId = alarm.getDcDataId();
        Long infoId = alarm.getInfoId();
        DcInfo info = iDcInfoService.find(infoId);
        DcData data = iDcDataService.find(dataId);
        String sfzh = info.getCardNo();
        DcPeople people = iDcPeopleService.findByCardNo(sfzh);

        JSONObject result = new JSONObject();
        result.put("alarm",alarm);
        result.put("data",data);
        result.put("info",info);
        result.put("people",people);


        return result.toJSONString();
    }




    @GetMapping(value = "/dc/alarms", produces = "application/json")
    public String alarms(HttpServletRequest request, Integer isRead,String startDate,String endDate) {
        CommonAdminuser adminuser = (CommonAdminuser) request.getSession().getAttribute("adminuser");
        if (null == isRead) {
            return null;
        }
        Timestamp start = null;
        Timestamp end = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (startDate!=null)
        {
            try {
                start = new Timestamp(sdf.parse(startDate).getTime());
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        if (endDate!=null)
        {
            try {
                end = new Timestamp(sdf.parse(endDate).getTime());
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        List<DcData> resultList = iDcAlarmService.list(isRead, adminuser.getId(),start,end);
//        List<DcData> resultList = new ArrayList<DcData>();
//        for (DcData data:tempList) {
//            boolean match = true;
//            if (data.getBuyTime()!=null)
//            {
//                if (start!=null && data.getBuyTime().getTime()<start.getTime())
//                {
//                    match = false;
//                }
//                if (end!=null && data.getBuyTime().getTime()>end.getTime()+24*60*60*1000)
//                {
//                    match = false;
//                }
//            }
//            else {
//                if (start!=null||end!=null)
//                {
//                    match = false;
//                }
//            }
//
//            if (match)
//            {
//
//                resultList.add(data);
//            }
//        }


        return UtilsUI.data(resultList);
    }






    @GetMapping(value = "/dc/check", produces = "application/json")
    public String check() {
        iDcAlarmService.check();
        return UtilsUI.ok();
    }
}